package ilincar.systemcore.ddpush;

import java.io.IOException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.security.MessageDigest;

/**
 * Created by ilincar on 2016/12/9.
 */

public class DDPushUtils {

    /**
     * DDPUSH 心跳间隔
     */
    public static final int DDPUSH_HEARTBEAT = 20;

    public static final String SERVER_IP = "serverIp";

    public static final String SERVER_PORT = "serverPort";

    public static final String PUSH_PORT = "pushPort";

    public static final String USER_NAME = "userName";

    public static final String SENT_PKGS = "sentPkgs";

    public static final String RECEIVE_PKGS = "receivePkgs";

    public static final int CUSTOM_NOTIFICATION_MESSAGE = 0x10;

    /**
     * 请求车机位置
     */
    public static final String DDPUSH_CAR_POSITION = "001";

    /**
     * 预导航
     */
    public static final String DDPUSH_PRENAVIGATION = "002";

    /**
     * 返回绑定状态
     */
    public static final String DDPUSH_BIND_DATA = "003";

    /**
     * 请求OBD数据
     */
    public static final String DDPUSH_OBD_DATA = "004";

    /**
     * 请求车机拍照
     */
    public static final String DDPUSH_TAKE_PHOTO = "005";

    /**
     * 微信接人
     */
    public static final String DDPUSH_WEIXIN_ACCESS = "010";

    /**
     * 请求上传视频文件
     */
    public static final String DDPUSH_REQUEST_VIDEO = "020";

    /**
     * 请求时时视频流
     */
    public static final String DDPUSH_LIVE_CAMERA_VIDEO = "033";

    /**
     * 停止时时视频流
     */
    public static final String DDPUSH_LIVE_CAMERA_STOP_VIDEO = "034";


    /***
     * 解密
     *
     * @param response
     * @return
     */
    public static String getEncodeAndGzip(String response) throws IOException {
        response = URLDecoder.decode(response, "UTF-8");
        return response;
    }

    /***
     * 加密操作
     *
     * @param json
     * @return
     */
    public static String setEncodeAndGizp(String json) throws IOException {
        // 加密
        json = URLEncoder.encode(json, "UTF-8");
        return json;
    }

    public static char convertDigit(int value) {
        value &= 0x0f;
        if (value >= 10)
            return ((char) (value - 10 + 'a'));
        else
            return ((char) (value + '0'));
    }

    public static String convert(final byte bytes[]) {

        StringBuffer sb = new StringBuffer(bytes.length * 2);
        for (int i = 0; i < bytes.length; i++) {
            sb.append(convertDigit((int) (bytes[i] >> 4)));
            sb.append(convertDigit((int) (bytes[i] & 0x0f)));
        }
        return (sb.toString());

    }

    public static String convert(final byte bytes[],int pos, int len) {

        StringBuffer sb = new StringBuffer(len * 2);
        for (int i = pos; i < pos+len; i++) {
            sb.append(convertDigit((int) (bytes[i] >> 4)));
            sb.append(convertDigit((int) (bytes[i] & 0x0f)));
        }
        return (sb.toString());

    }

    public static byte[] md5Byte(String encryptStr) throws Exception {
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(encryptStr.getBytes("UTF-8"));
        return md.digest();
    }

    public static String md5(String encryptStr) throws Exception {
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(encryptStr.getBytes("UTF-8"));
        byte[] digest = md.digest();
        StringBuffer md5 = new StringBuffer();
        for (int i = 0; i < digest.length; i++) {
            md5.append(Character.forDigit((digest[i] & 0xF0) >> 4, 16));
            md5.append(Character.forDigit((digest[i] & 0xF), 16));
        }

        encryptStr = md5.toString();
        return encryptStr;
    }

}
